using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 对象池 : MonoBehaviour
{
    public static 对象池 _Instance;
    private const int maxNumber = 100;
    public GameObject[] prefabObjects;
    private Dictionary<string, List<GameObject>> pool = new Dictionary<string, List<GameObject>>();
    public static class PrefabNames
    {
        public static string 箭 = "arrow";
        public static string 金币 = "gold";
        public static string 玩家 = "Player";
        public static string 炸弹 = "bomb";
        public static string 爆炸效果 = "boom";
        public static string UI炸弹图片 = "UIBomb";
        public static string 巨型哥布林_石头 = "cyclop_stone";
    }
    private void Awake()
    {
        _Instance = this;
        
    }
    public GameObject GetOut(string objectName,Vector3 position)
    {
        GameObject gameObject;
        if (pool.ContainsKey(objectName) && pool[objectName].Count > 0)
        {
            gameObject = pool[objectName][0];
            gameObject.transform.position = position;
            gameObject.SetActive(true);

            pool[objectName].RemoveAt(0);

        }
        else
        {
            GameObject prefabObject=null;
            for(int i = 0; i < prefabObjects.Length; i++)
            {
                if (prefabObjects[i].name == objectName)
                {
                    prefabObject = prefabObjects[i];
                }
            }
            gameObject = GameObject.Instantiate(prefabObject, position, Quaternion.identity);
            
        }
        gameObject.transform.SetParent(null);
        
        return gameObject;
    }
    public void SetIn(string objectName,GameObject gameObject)
    {
        
        if (!pool.ContainsKey(objectName))
        {
            pool.Add(objectName, new List<GameObject>());
            
        }
        Debug.Log(objectName + pool[objectName].Count);
        gameObject.transform.parent = transform;
        gameObject.SetActive(false);
        if (pool[objectName].Count <= maxNumber) pool[objectName].Add(gameObject);
        else
        {
            Destroy(gameObject);
            
        }

        
    }
}
